/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package PictureDescription;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.Timer;
import GuiTools.*;
import Mainframe.Mainframe;
import PDPictureEditor.*;
import Settings.*;

/**
 * @author Christoph Schnack
 *
 */
public class PictureViewer implements PicturePanelParent{
	TribuitFrame pvFrame;
	JComboBox zoomCB;
	Language LANG;
	Mainframe MAINFRAME;
	Settings SET;
	PictureUnit PU;
	PicturePanel picturePanel;
	Vector<PNUComponent> pnups = new Vector<PNUComponent>();
	String colName;
	boolean hasTags = false,highlighting=false,highlightSwitch=false;
	int hlIndex;
	Timer flashTimer;
	
	public PictureViewer(Settings set,Mainframe mf,String cName, PictureUnit pu){
		SET = set;
		MAINFRAME = mf;
		LANG = set.getLanguage();
		PU = pu;
		colName = cName;
		hasTags = pu.hasPictureNameUnits();
		buildGUI();
	}
	
	public void buildGUI(){
		flashTimer = new Timer(600,new FlashTimerListener());
		pvFrame = new TribuitFrame(LANG.getPictureViewer() + " - " + PU.getName());
		
		picturePanel = new PicturePanel(SET,pvFrame,this);
		
		JLabel nameLabel = new JLabel(LANG.getName()+":");
		nameLabel.setFont(new Font(Font.SANS_SERIF,Font.BOLD,14));
		JLabel nameL = new JLabel(PU.getName());
		nameL.setToolTipText(PU.getName());
		nameL.setOpaque(false);
		nameLabel.setOpaque(false);
		JLabel titleLabel = new JLabel(LANG.getTitle()+":");
		titleLabel.setFont(new Font(Font.SANS_SERIF,Font.BOLD,14));
		JLabel titleL = new JLabel(PU.getTitle());
		titleL.setToolTipText(PU.hasTitle()?PU.getTitle():"-");
		titleL.setOpaque(false);
		titleLabel.setOpaque(false);
		JLabel collectionLabel = new JLabel(LANG.getCollection()+":");
		collectionLabel.setFont(new Font(Font.SANS_SERIF,Font.BOLD,14));
		JLabel collectionL = new JLabel(colName);
		collectionL.setToolTipText(colName);
		collectionL.setOpaque(false);
		collectionLabel.setOpaque(false);
		JLabel sectionLabel = new JLabel(LANG.getSection()+":");
		sectionLabel.setFont(new Font(Font.SANS_SERIF,Font.BOLD,14));
		JLabel sectionL = new JLabel(PU.getSection());
		sectionL.setToolTipText(PU.getSection());
		sectionL.setOpaque(false);
		sectionLabel.setOpaque(false);
		nameLabel.setMinimumSize(nameLabel.getPreferredSize());
		titleLabel.setMinimumSize(titleLabel.getPreferredSize());
		collectionLabel.setMinimumSize(collectionLabel.getPreferredSize());
		sectionLabel.setMinimumSize(sectionLabel.getPreferredSize());
		
		JPanel infoPanel = new JPanel(new GridBagLayout());
		infoPanel.setBorder(BorderFactory.createTitledBorder(LANG.getMetainformation()));
		//EditButton tInfoBtn = new EditButton(GuiTools.loadImageFromGraphics("infoIcon.png"),GuiTools.loadImageFromGraphics("infoIconHovered.png"),GuiTools.loadImageFromGraphics("infoIconDisabled.png"),PU.hasTitle());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.NONE;
		gbc.weightx = 0;
		gbc.gridy=1;
		gbc.gridx=1;
		infoPanel.add(nameLabel,gbc);
		++gbc.gridy;
		infoPanel.add(titleLabel,gbc);
		++gbc.gridy;
		infoPanel.add(collectionLabel,gbc);
		++gbc.gridy;
		infoPanel.add(sectionLabel,gbc);
		gbc.gridy=1;
		gbc.gridx=2;
		gbc.weightx=3;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0,5,0,0);
		gbc.anchor = GridBagConstraints.WEST;
		infoPanel.add(nameL,gbc);
		++gbc.gridy;
		infoPanel.add(titleL,gbc);
		++gbc.gridy;
		infoPanel.add(collectionL,gbc);
		++gbc.gridy;
		infoPanel.add(sectionL,gbc);
		infoPanel.setPreferredSize(new Dimension(200,infoPanel.getPreferredSize().height));
		
		
		JButton stopFlashBtn = new JButton(LANG.getStopFlash());
		stopFlashBtn.setEnabled(hasTags);
		stopFlashBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				stopHighlightTags();
			}
		});
		JButton hideAllTagsBtn = new JButton(LANG.getHideAll());
		hideAllTagsBtn.setEnabled(hasTags);
		hideAllTagsBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				for(int i=0;i<pnups.size();i++){
					pnups.elementAt(i).setHideTags(true);
				}
				picturePanel.repaintImagePanel();
			}
		});
		
		JButton showAllTagsBtn = new JButton(LANG.getDisplayAll());
		showAllTagsBtn.setEnabled(hasTags);
		showAllTagsBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				for(int i=0;i<pnups.size();i++){
					pnups.elementAt(i).setHideTags(false);
				}
				picturePanel.repaintImagePanel();
			}
		});
		JPanel tagButtonsPanel = new JPanel();
		tagButtonsPanel.setOpaque(false);
		tagButtonsPanel.add(hideAllTagsBtn);
		tagButtonsPanel.add(showAllTagsBtn);
		tagButtonsPanel.add(stopFlashBtn);
		JPanel pnuPanel = new JPanel();
		pnuPanel.setBackground(Color.white);
		pnuPanel.setLayout(new GridBagLayout());
		if(hasTags){
			GridBagConstraints pnuGbc = new GridBagConstraints();
			pnuGbc.fill = GridBagConstraints.BOTH;
			pnuGbc.anchor = GridBagConstraints.NORTHWEST;
			pnuGbc.weighty=0;
			pnuGbc.weightx=2;
			pnuGbc.insets = new Insets(5,0,0,0);
			PNUPanel p;
			Vector<PictureNameUnit> pnus = PU.getTags();
			for(int i=0;i<pnus.size();i++){
				p = new PNUPanel(pnus.elementAt(i),i);
				pnuGbc.gridy++;
				pnuPanel.add(p,pnuGbc);
				pnups.add(p);
			}
		}
		JScrollPane jsp = new JScrollPane(pnuPanel);
		jsp.getVerticalScrollBar().setUnitIncrement(50);
		jsp.setPreferredSize(new Dimension(250,300));
		JPanel tagPanel = new JPanel(new GridBagLayout());
		GridBagConstraints gb = new GridBagConstraints();
		gb.fill =GridBagConstraints.BOTH;
		gb.anchor =GridBagConstraints.NORTHWEST;
		gb.weighty = 1;
		tagPanel.add(jsp,gb);
		gb.weighty = 0;
		gb.gridy = 1;
		tagPanel.add(tagButtonsPanel,gb);
		tagPanel.setBorder(BorderFactory.createTitledBorder(LANG.getTags()+":"));
		
		final JComboBox zoomCB = new JComboBox(GuiTools.getZOOMSTEPS());
		zoomCB.setSelectedItem("1.00");
		zoomCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				picturePanel.setZoom(Double.parseDouble((String)zoomCB.getSelectedItem()));
			}
		});
		LabelPanel zoomLP = new LabelPanel("zoom",zoomCB);
		zoomLP.setOpaque(false);
		
		JPanel tagButtonsPanelBottom = new JPanel(new FlowLayout(FlowLayout.LEFT));
		tagButtonsPanelBottom.setOpaque(false);
		tagButtonsPanelBottom.add(zoomLP);
		
		JPanel westPanel = new JPanel();
		westPanel.setBackground(Color.gray);
		westPanel.setLayout(new GridBagLayout());
		gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10,10,0,10);
		gbc.gridy = 1;
		gbc.weighty = 0;
		gbc.weightx=1;
		westPanel.add(infoPanel,gbc);
		++gbc.gridy;
		gbc.weighty = 2;
		westPanel.add(tagPanel,gbc);
		++gbc.gridy;
		gbc.weighty = 0;
		gbc.insets = new Insets(10,10,10,10);
		westPanel.add(tagButtonsPanelBottom,gbc);
		
		TScrollPane westTSP =  new TScrollPane(westPanel);
		westTSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		JButton closeBTN = new JButton(LANG.getClose());
		closeBTN.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pvFrame.dispose();
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		buttonPanel.add(closeBTN);
		pvFrame.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getPictureViewer()));
		pvFrame.getContentPane().add(BorderLayout.CENTER, picturePanel);
		pvFrame.getContentPane().add(BorderLayout.WEST, westTSP);
		pvFrame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
		pvFrame.setResizable(true);
		pvFrame.showUp(50,50);
		try{
			picturePanel.setImage(ImageIO.read(new File(GuiTools.PATHTOPICTUREMAPS+File.separator+colName+File.separator+PU.getName()+File.separator+"image")));
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	
	class PNUPanel extends JPanel implements PNUComponent{
		Vector<PictureTagUnit> positions;
		String name;
		int index;
		PictureNameUnit pnu;
		boolean hideTags = false,infoPanelIsShown = false;
		JPanel panel,infoPanel;
		JCheckBox hideTagsCB;
		JScrollPane infoPane;
		
		public PNUPanel(PictureNameUnit p,int ind){
			super();
			panel = this;
			pnu = p;
			positions = pnu.getTags();
			name = pnu.getName();
			index = ind;
			buildGUI();
		}
		
		private void buildGUI(){
			JLabel tagName = new JLabel(name);
			tagName.setFont(new Font(Font.SANS_SERIF,Font.BOLD,13));
			JLabel indexLabel = new JLabel(""+(index+1));
			JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
			namePanel.add(indexLabel);
			namePanel.add(tagName);
			indexLabel.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));
			hideTagsCB = new JCheckBox(LANG.getHideTags());
			hideTagsCB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					hideTags = hideTagsCB.isSelected();
					picturePanel.repaintImagePanel();
				}
			});
			JButton flashButton = new JButton(LANG.getFlash());
			flashButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					highlightTags(index);
				}
			});
			infoPanel = new JPanel();
			CustomTextArea infoCTA = new CustomTextArea(pnu.getInfoText());
			infoPane = new JScrollPane(infoCTA);
			infoPane.setPreferredSize(new Dimension(200,100));
			JButton infoButton = new JButton();
			try{
				infoButton.setIcon(new ImageIcon(ImageIO.read(new File("graphics"+File.separator+"infoIcon.png"))));
			}
			catch(IOException e){
				e.printStackTrace();
			}
			infoButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					infoPanel.removeAll();
					if(!infoPanelIsShown){
						infoPanel.add(infoPane);
						infoPanelIsShown = true;
					}
					else infoPanelIsShown = false;
					pvFrame.setPreferredSize(pvFrame.getSize());
					pvFrame.pack();
				}
			});
			infoButton.setEnabled(pnu.hasInfoText());
			JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
			buttonsPanel.add(flashButton);
			buttonsPanel.add(infoButton);
			buttonsPanel.add(hideTagsCB);
			panel.setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor = GridBagConstraints.NORTHWEST;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.weightx = 1;
			panel.add(namePanel,gbc);
			gbc.gridy = 1;
			panel.add(infoPanel,gbc);
			gbc.gridy = 2;
			panel.add(buttonsPanel,gbc);
			
		}
		public boolean getHideTags() {
			return hideTags;
		}
		
		public void setHideTagsCBEnabled(boolean arg){
			hideTagsCB.setEnabled(arg);
		}
		
		public void setHideTags(boolean arg) {
			hideTags = arg;
			hideTagsCB.setSelected(arg);
		}
		
		public boolean getShouldHideTags() {
			// TODO Auto-generated method stub
			return false;
		}
		
		public boolean hasTags() {
			pnu.hasTags();
			return false;
		}
		
		public int getIndex() {
			return index;
		}
		
		public void setIndex(int i) {
			index = i;
		}
		
		
		public Vector<PictureTagUnit> getPictureTags() {return positions;}
		public void addPictureTagUnit(PictureTagUnit ptu) {}
		public void disableDeleteTagButton() {}
		public void selectNothing() {}
		public void setAddTagButtonActive(boolean arg) {}
		public void setPictureTagUnit(int index, PictureTagUnit ptu) {}
		public void updateTagArmPanel() {}
	}

	

	public Vector<PNUComponent> getPictureNameUnits() {
		return pnups;
	}

	public boolean getShowAllTags() {
		return false;
	}
	
	public void highlightTags(int index){
		hlIndex=index;
		highlighting=true;
		picturePanel.setHighlighting(true);
		picturePanel.setHlIndex(hlIndex);
		picturePanel.setHighlightVector(true);
		flashTimer.start();
	}
	class FlashTimerListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			highlightSwitch=highlightSwitch?false:true;
			picturePanel.setHighlightSwitch(highlightSwitch?false:true);
			picturePanel.repaintImagePanel();
		}
	}
	
	public void stopHighlightTags(){
		flashTimer.stop();
		picturePanel.setHighlighting(false);
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				picturePanel.repaintImagePanel();
			}
		});
	}
	
	public void addPictureTagUnitTo(PictureTagUnit ptu, int setTagIndex) {}
	public void setPictureTagUnit(int index, int ptuIndex, PictureTagUnit pu) {}
	public void setTagArm(int ind, int tagInd, int armInd, TADE tade) {}
}
